package com.example.matchingengine.service.model;

import com.example.matchingengine.model.Order;
import com.example.matchingengine.model.Order.OrderSide;
import com.example.matchingengine.model.Order.OrderStatus;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 订单快照
 * 用于订单数据的持久化和恢复
 */
@Data
public class OrderSnapshot {
    private String id;
    private String userId;
    private String symbol;
    private OrderSide side;
    private Order.OrderType type;
    private Order.TimeInForce timeInForce;
    private BigDecimal price;
    private BigDecimal quantity;
    private BigDecimal filledQuantity;
    private OrderStatus status;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;

    /**
     * 从Order对象创建快照
     */
    public static OrderSnapshot fromOrder(Order order) {
        OrderSnapshot snapshot = new OrderSnapshot();
        snapshot.setId(order.getId());
        snapshot.setUserId(order.getUserId());
        snapshot.setSymbol(order.getSymbol());
        snapshot.setSide(order.getSide());
        snapshot.setType(order.getType());
        snapshot.setTimeInForce(order.getTimeInForce());
        snapshot.setPrice(order.getPrice());
        snapshot.setQuantity(order.getQuantity());
        snapshot.setFilledQuantity(order.getFilledQuantity());
        snapshot.setStatus(order.getStatus());
        snapshot.setCreateTime(order.getCreateTime());
        snapshot.setUpdateTime(order.getUpdateTime());
        return snapshot;
    }

    /**
     * 转换为Order对象
     */
    public Order toOrder() {
        Order order = new Order();
        order.setId(this.id);
        order.setUserId(this.userId);
        order.setSymbol(this.symbol);
        order.setSide(this.side);
        order.setType(this.type != null ? this.type : Order.OrderType.LIMIT);
        order.setTimeInForce(this.timeInForce != null ? this.timeInForce : Order.TimeInForce.GTC);
        order.setPrice(this.price);
        order.setQuantity(this.quantity);
        order.setFilledQuantity(this.filledQuantity);
        order.setStatus(this.status);
        order.setCreateTime(this.createTime);
        order.setUpdateTime(this.updateTime);
        return order;
    }
}